package cc.mrbird.febs.pedometer.service.impl;

import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.pedometer.entity.Batches;
import cc.mrbird.febs.pedometer.mapper.BatchesMapper;
import cc.mrbird.febs.pedometer.mapper.ChickenMapper;
import cc.mrbird.febs.pedometer.service.IBatchesService;
import cc.mrbird.febs.system.entity.Role;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 鸡批次 Service实现
 *
 * @author li
 * @date 2020-01-31 11:40:37
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BatchesServiceImpl extends ServiceImpl<BatchesMapper, Batches> implements IBatchesService {

    @Autowired
    private BatchesMapper batchesMapper;

    @Override
    public IPage<Batches> findBatchess(QueryRequest request, Batches batches) {
        LambdaQueryWrapper<Batches> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<Batches> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Batches> findBatchess(Batches batches) {
	    LambdaQueryWrapper<Batches> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createBatches(Batches batches) {
        this.save(batches);
    }

    @Override
    @Transactional
    public void updateBatches(Batches batches) {
        if (batches.getFile().equals("")){
            batches.setFile(null);
        }
        baseMapper.updateById(batches);
    }

    @Override
    @Transactional
    public void deleteBatches(String batchesId) {
        List<String> list = Arrays.asList(batchesId.split(StringPool.COMMA));
        this.baseMapper.delete(new QueryWrapper<Batches>().lambda().in(Batches::getBatchesId, list));
	}
}
